import { Component, Input, OnInit } from '@angular/core';
import { NzModalRef, NzMessageService } from 'ng-zorro-antd';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { OrderService } from '@core/routes-services/order.service';
import { MoonCakeService } from '@core/routes-services/moon-cake.service';
import {
  CAKE_ORDER_SERVICE_STATUS_OPTION,
  CAKE_ORDER_STATUS_OPTION,
  CAKE_ORDER_VOUCHER_STATUS_OPTION,
} from '../../../../constant/dictionary';

@Component({
  selector: 'app-order-view',
  templateUrl: './cake-order-view.component.html',
  styles: [`
    h2 {
      border-bottom: 1px solid #1890ff;
      color: #1890ff;
      padding-top: 10px;
    }
  `],
})
export class CakeOrderViewComponent implements OnInit {
  entity: any = {};
  validateForm: FormGroup;
  isLoading: boolean = false;
  statusOption: any [] = [];

  cakeOrderStatusOption: any [] = CAKE_ORDER_STATUS_OPTION;
  cakeOrderServiceStatusOption: any [] = CAKE_ORDER_SERVICE_STATUS_OPTION;
  cakeOrderVoucherStatusOption: any [] = CAKE_ORDER_VOUCHER_STATUS_OPTION;

  viewType: any = 1;//1-预定订单（默认） 2-月饼发货

  allProducts: any [] = [];

  redpackRewards: any [] = [];
  integralRewards: any [] = [];
  profitRecordInfos: any [] = [];
  achievementRecordInfos: any [] = [];

  constructor(private fb: FormBuilder,
              private modal: NzModalRef,
              private orderService: OrderService,
              private moonCakeService: MoonCakeService,
              public message: NzMessageService) {
  }

  ngOnInit(): void {
    console.log(this.entity);
    this.isLoading = true;
    this.validateForm = this.fb.group({
      name: [this.entity.name, [Validators.required, Validators.maxLength(30)]],
    });
    let that = this;
    setTimeout(function(res) {
      that.isLoading = false;
    }, 300);

    if (this.entity.companyId) {
      this.initRewards();
    }
  }

  initRewards() {
    this.moonCakeService.orderRewards(this.entity.id).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.redpackRewards = res.data.redpackRewards ? res.data.redpackRewards : [];
        this.integralRewards = res.data.integralRewards ? res.data.integralRewards : [];
        this.profitRecordInfos = res.data.profitRecordInfos ? res.data.profitRecordInfos : [];
        this.achievementRecordInfos = res.data.achievementRecordInfos ? res.data.achievementRecordInfos : [];
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  submitForm(): void {
    if (this.validateForm.invalid) {
      for (const i in this.validateForm.controls) {
        this.validateForm.controls[i].markAsDirty();
        this.validateForm.controls[i].updateValueAndValidity();
      }
    } else {
      this.isLoading = true;
      this.orderService.saveOrUpdateEntity(this.entity.id, this.validateForm.value).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.modal.close(true);
        } else {
          this.message.warning(res.message);
        }
        this.isLoading = false;
      }, error => {
        this.message.error(error.message ? error.message : error.error);
        this.isLoading = false;
      });
    }
  }

  close() {
    this.modal.destroy();
  }


  picList: any [] = [];
  isShowPic: boolean = false;

  showPic(key) {
    this.picList = [];
    this.picList.push({ imgKey: key });
    this.isShowPic = true;
  }

  onPicClose(res) {
    this.isShowPic = res;
    this.isShowPic = false;
  }

  //是否在重复中，防止重复发放
  reSending: boolean = false;
  reSend = (data) => {
    if(!this.reSending){
      this.reSending = true;
      this.moonCakeService.reSendRedpack(data.id, {}).then((res) => {
        if (res.status.toString().startsWith('20')) {
          this.initRewards();
          this.message.success('补发成功');
        } else {
          this.message.warning(res.message);
        }
        this.reSending = false;
      }, error => {
        this.message.warning('补发失败，请重试，或联系管理员');
        this.reSending = false;
        console.error(error);
      });
    }
  };
}
